Skip to content

How do I use JSON with Minizinc? #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mrakgr opened this issue May 29, 2025 · 2 comments
Open

How do I use JSON with Minizinc? #204

mrakgr opened this issue May 29, 2025 · 2 comments

Comments

@mrakgr
Copy link

mrakgr commented May 29, 2025

I have no idea how to use enum fields in non-.dzn files. When I try to run minizinc prototype.mzn prototype.json I get the Error: type error: initialisation value for predictions' has invalid type-inst: expected array[_] of record(MODELS: model, float: price, float: workload)', actual array[_] of record(string: model, int: price, int: workload)'`. Here is the file:

{
    "MODELS": ["LLAMA4_SMALL_ON_OPENROUTER", "LLAMA4_MEDIUM_ON_S19", "SOME_NEW_LARGE_MODEL_ON_UNKNOWN_MARKETPLACE"],
    "MACHINES": ["M1", "M2", "M3"],
    "TYPES": ["HGX"],
    "gpus_per_model": [1, 1, 1],
    "machines": [
        {"gpus": 8, "machine_type": "HGX", "cost_per_gpu": 0.0001},
        {"gpus": 8, "machine_type": "HGX", "cost_per_gpu": 0.0001},
        {"gpus": 8, "machine_type": "HGX", "cost_per_gpu": 0.0001}
    ],
    "workload_per_type_per_model": [[3.0, 2.0, 1.0]],
    "predictions": [
        {"model": "LLAMA4_SMALL_ON_OPENROUTER", "price": 10, "workload": 10},
        {"model": "LLAMA4_MEDIUM_ON_S19", "price": 10, "workload": 10},
        {"model": "SOME_NEW_LARGE_MODEL_ON_UNKNOWN_MARKETPLACE", "price": 10, "workload": 10}
    ]
}
@mrakgr
Copy link
Author

mrakgr commented May 29, 2025

The original .dzn file is this:

MODELS = {LLAMA4_SMALL_ON_OPENROUTER, LLAMA4_MEDIUM_ON_S19, SOME_NEW_LARGE_MODEL_ON_UNKNOWN_MARKETPLACE};
MACHINES = {M1, M2, M3};
TYPES = {HGX};

gpus_per_model = [1, 1, 1];
machines = [
    (gpus: 8, machine_type: HGX, cost_per_gpu: 0.0001), 
    (gpus: 8, machine_type: HGX, cost_per_gpu: 0.0001), 
    (gpus: 8, machine_type: HGX, cost_per_gpu: 0.0001)
    ];
workload_per_type_per_model = [|3.0, 2.0, 1.0|];

predictions = [
    (model: LLAMA4_SMALL_ON_OPENROUTER, price: 10, workload: 10), 
    (model: LLAMA4_MEDIUM_ON_S19, price: 10, workload: 10), 
    (model: SOME_NEW_LARGE_MODEL_ON_UNKNOWN_MARKETPLACE, price: 10, workload: 10),
    ];

@mrakgr
Copy link
Author

mrakgr commented May 29, 2025

Could I do the same thing via the Python interface somehow? I tried the following amongst others and it failed with the same error.

from enum import Enum
from minizinc import Instance, Model, Solver

# Load the model
model = Model('/home/mrakgr/inference-servers/icf/prototype.mzn')

# Create a solver instance
gecode = Solver.lookup('gecode')
instance = Instance(gecode, model)

models = Enum("MODELS", ["LLAMA4_SMALL_ON_OPENROUTER", "LLAMA4_MEDIUM_ON_S19", "SOME_NEW_LARGE_MODEL_ON_UNKNOWN_MARKETPLACE"])
machines = Enum("MACHINES", ["M1", "M2", "M3"])
types = Enum("TYPES", ["HGX"])

data = {
    "MODELS": models,
    "MACHINES": machines,
    "TYPES": types,
    "gpus_per_model": [1, 1, 1],
    "machines": [
        {"gpus": 8, "machine_type": "HGX", "cost_per_gpu": 0.0001},
        {"gpus": 8, "machine_type": "HGX", "cost_per_gpu": 0.0001},
        {"gpus": 8, "machine_type": "HGX", "cost_per_gpu": 0.0001}
    ],
    "workload_per_type_per_model": [[3.0, 2.0, 1.0]],
    "predictions": [
        {"model": "LLAMA4_SMALL_ON_OPENROUTER", "price": 10, "workload": 10},
        {"model": "LLAMA4_MEDIUM_ON_S19", "price": 10, "workload": 10},
        {"model": "SOME_NEW_LARGE_MODEL_ON_UNKNOWN_MARKETPLACE", "price": 10, "workload": 10}
    ]
}

for k,v in data.items():
    instance[k] = v

# Solve the instance
result = instance.solve()

# Print the result
print(result)

# python run.py
(main) mrakgr@Marko:~/inference-servers$  cd /home/mrakgr/inference-servers ; /usr/bin/env /home/mrakgr/.venv/main/bin/python /home/mrakgr/.vscode-server/extensions/ms-python.debugpy-2025.8.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher 49553 -- /home/mrakgr/inference-servers/icf/run.py 
Traceback (most recent call last):
  File "/usr/lib/python3.12/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/mrakgr/.vscode-server/extensions/ms-python.debugpy-2025.8.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 71, in <module>
    cli.main()
  File "/home/mrakgr/.vscode-server/extensions/ms-python.debugpy-2025.8.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 501, in main
    run()
  File "/home/mrakgr/.vscode-server/extensions/ms-python.debugpy-2025.8.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 351, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/home/mrakgr/.vscode-server/extensions/ms-python.debugpy-2025.8.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 310, in run_path
    return _run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mrakgr/.vscode-server/extensions/ms-python.debugpy-2025.8.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 127, in _run_module_code
    _run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name)
  File "/home/mrakgr/.vscode-server/extensions/ms-python.debugpy-2025.8.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 118, in _run_code
    exec(code, run_globals)
  File "/home/mrakgr/inference-servers/icf/run.py", line 37, in <module>
    result = instance.solve()
             ^^^^^^^^^^^^^^^^
  File "/home/mrakgr/.venv/main/lib/python3.12/site-packages/minizinc/instance.py", line 193, in solve
    return asyncio.run(coroutine)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/mrakgr/.venv/main/lib/python3.12/site-packages/minizinc/instance.py", line 248, in solve_async
    async for result in self.solutions(
  File "/home/mrakgr/.venv/main/lib/python3.12/site-packages/minizinc/instance.py", line 618, in solutions
    async for obj in decode_async_json_stream(
  File "/home/mrakgr/.venv/main/lib/python3.12/site-packages/minizinc/json.py", line 122, in decode_async_json_stream
    raise error_from_stream_obj(obj)
minizinc.error.TypeError: initialisation value for `predictions' has invalid type-inst: expected `array[_] of record(MODELS: model, float: price, float: workload)', actual `array[_] of record(string: model, int: price, int: workload)'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant